﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>DevGridControlDrawCheckBox</Title>
    <Author>作者</Author>
    <Shortcut>DevGridControlDrawCheckBox</Shortcut>
    <Description>制作GridView列标题为复选框</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>DevGridControlDrawCheckBox</ID>
        <Default>DevGridControlDrawCheckBox</Default>
      </Literal>
    </Declarations>
    <Code Language="XML">
      <![CDATA[/*此方法调用在
        private void gvErrors_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName.Equals("Enable"))
            {
                e.Info.InnerElements.Clear();
                e.Painter.DrawObject(e.Info);
                DrawCheckBox(e, _checkAll);
                e.Handled = true;
            }
        }
        */
        private void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
        {
            RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as RepositoryItemCheckEdit;
            if (repositoryCheck != null)
            {
                Graphics g = e.Graphics;
                Rectangle r = e.Bounds;
                DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
                DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
                DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
                info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
                painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
                info.EditValue = chk;
                info.Bounds = r;
                info.CalcViewInfo(g);
                args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
                painter.Draw(args);
                args.Cache.Dispose();
            }
        }
        /* 全选示意
        private void gvErrors_Click(object sender, EventArgs e)
        {
            var pt = gcErrors.PointToClient(Control.MousePosition);
            var info = gvErrors.CalcHitInfo(pt);
            if (info.InColumn && info.Column != null && info.Column.FieldName.Equals("Enable"))
            {
                var errors = gcErrors.DataSource as List<ErrorWarningDTO>;
                if (errors != null)
                {
                    errors.ForEach(x => x.Enable = !_checkAll);
                }
                gcErrors.RefreshDataSource();
            }
            _checkAll = !_checkAll;
        }

        private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
        {
            var ctr = sender as CheckEdit;
            if (gvErrors.RowCount < 1 || ctr == null)
                return;
            bool br = true;
            var index = gvErrors.FocusedRowHandle;
            for (int i = 0; i < gvErrors.RowCount; i++)
            {
                if (index == i)
                    continue;
                var value = (bool)gvErrors.GetRowCellValue(i, "Enable");
                br = br && value;
            }
            br = br && ctr.Checked;
            _checkAll = br;
            gvErrors.InvalidateColumnHeader(gvErrors.Columns["Enable"]);
        }
        */
      ]]>
    </Code>
  </Snippet>
</CodeSnippet>